package com.example.vue_shop.utils;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

public class ResultObj {
    private boolean status = true;
    private Map<String, Object> data;
    private String message;


    private static volatile ResultObj resultObj = null;

    private ResultObj() {

    }

    /**
     * 成功返回方法.
     * @param data 返回数据
     * @param message 返回消息.
     * @return String
     */
    public static String success(Map<String, Object> data, String message) {
        if (resultObj == null) {
            synchronized (ResultObj.class) {
                if (resultObj == null) {
                    resultObj = new ResultObj();
                }
            }
        }

        resultObj.setData(data);
        resultObj.setMessage(message);
        return JSON.toJSONString(resultObj);
    }

    public static String success(String message) {
        if (resultObj == null) {
            synchronized (ResultObj.class) {
                if (resultObj == null) {
                    resultObj = new ResultObj();
                }
            }
        }
        resultObj.setData(new HashMap<>());
        resultObj.setMessage(message);
        return JSON.toJSONString(resultObj);
    }

    /**
     * 失败方法.
     * @param message 返回信息
     * @return String
     */
    public static String error(String message) {
        if (resultObj == null) {
            synchronized (ResultObj.class) {
                if (resultObj == null) {
                    resultObj = new ResultObj();
                }
            }
        }
        resultObj.setStatus(false);
        resultObj.setData(new HashMap<>());
        resultObj.setMessage(message);
        return JSON.toJSONString(resultObj);
    }


    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static ResultObj getResultObj() {
        return resultObj;
    }

    public static void setResultObj(ResultObj resultObj) {
        ResultObj.resultObj = resultObj;
    }
}
